/**
 * 首页
 * @author yaohuan
 * @date 2019/03/19
 */
define(['plugins/echarts/echarts.min'], function (echarts) {

    var self,
        app = win.app,
        list = [],//1、组织公告；
        channelList = [], //2、渠道公告；
        DistributorList = [], //3、经销商公告
        imgList1 = [], //产品展示
        imgList2 = [],
        shopping = [],
        list = [],
        swiper, swiper2, swiper3;


    var Admin = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _loadTplsFull: [
                    {
                        dom: 'notice_detail', tpl: 'system/notice_detail'
                    },
                    {
                        dom: 'goods-info', tpl: 'system/goods-info'
                    }, {
                        dom: 'shopCar_order', tpl: 'system/shopCar_order'
                    }, {
                        dom: 'common_open_window', tpl: 'common/_open_window'
                    },
                    {
                        dom: 'common_open_gwdc001', tpl: 'common/_open_gwdc001'
                    }]
            }, win);


        },
        //初始化数据
        initData: function () {
            //下面的mark不要删除 add by ganjia
            /*var myChart = echarts.init(document.getElementById('chart'));
             var names = [];
             names[0] = '潜在客户';
             names[1] = '意向客户';
             names[2] = '立项客户';
             names[3] = '商务客户';
             names[4] = '成交客户';
             app.ajax({
             xurl: "crm/gwat160/getCount",
             cb: function (result) {
             if (result.ok) {
             var datas = [];
             datas[4] = result.data.data.count1;
             datas[3] = result.data.data.count2;
             datas[2] = result.data.data.count3;
             datas[1] = result.data.data.count4;
             datas[0] = result.data.data.count5;
             document.getElementById('td1').innerHTML = names[0] + ' ' + datas[4] + '家';
             document.getElementById('td2').innerHTML = names[1] + ' ' + datas[3] + '家';
             document.getElementById('td3').innerHTML = names[2] + ' ' + datas[2] + '家';
             document.getElementById('td4').innerHTML = names[3] + ' ' + datas[1] + '家';
             document.getElementById('td5').innerHTML = names[4] + ' ' + datas[0] + '家';
             var option = {
             title: {
             text: '销售漏斗(Sales Funnel)'
             },
             legend: {
             x: 0,
             y: 25,
             data: names
             },
             calculable: true,
             series: [
             {
             name: '销售漏斗(Sales Funnel)',
             type: 'funnel',
             left: '0px',
             top: 80,
             bottom: 60,
             width: '80%',
             minSize: '0%',
             maxSize: '100%',
             sort: 'none',
             gap: 2,
             label: {
             show: false,
             position: 'inside'
             },
             data: [
             {value: 100,name:names[0]},
             {value: 80,name:names[1]},
             {value: 60,name:names[2]},
             {value: 40,name:names[3]},
             {value: 20,name:names[4]}
             ]
             }
             ]
             };
             myChart.setOption(option);
             }
             }
             });*/

            app.ajax({
                xurl: "crm/gwai500/getProductList",
                xdata: {GWAM028: '1'},
                cb: function (result) {
                    if (result.ok) {
                        imgList1.pushArray(result.data.data);
                        imgList1.forEach(function (v) {
                            v.GWAM015 = v.GWAM015.split(',')[0];
                        })
                    }
                }
            });
            app.ajax({
                xurl: "crm/gwai500/getProductList",
                xdata: {GWAM028: '3'},
                cb: function (result) {
                    if (result.ok) {
                        imgList2.pushArray(result.data.data);
                        imgList2.forEach(function (v) {
                            v.GWAM015 = v.GWAM015.split(',')[0];
                        })
                    }
                }
            });
        },
        _ready: function () {
            new Vue({
                el: '#admin',
                data: {
                    lists: list,
                    cList: channelList,
                    dList: DistributorList,
                    imgLists1: imgList1,
                    imgLists2: imgList2,
                    activeName2: 'first',
                    currentPage: 0,
                    currentPage2: 0,
                    currentPage3: 0,
                    currentPage4: 0,
                    pageSize: 5,
                    pageSize4: 100,
                    total4: 0,
                    pageData: [],
                    pageData2: [],
                    pageData3: [],
                    pageData4: [],
                    formInline: {
                        GWAM001: '',
                        GWAML004: '',
                        GWDC001: '',
                        GWAK061: '',
                        GWAM004: '',
                        GWAM004_NAME: '',
                        GWAM007: '',
                        GWAM007_NAME: '',
                        GWAK001: '',
                        GWAKL003: '',
                        GWAK010: ''
                    },
                    disabled: false,
                    hidden: false,
                    shopping: shopping,
                    list: []
                },
                methods: {

                    shopInfo: function (obj) {
                        obj.GWAK001 = this.formInline.GWAK001;
                        obj.GWAK010 = this.formInline.GWAK010;
                        self._openTplFull({
                            tag: 'goods-info',
                            tagData: obj,
                            index: 1,
                            openOpts: {
                                title: '商品详情',
                                area: ['900px', '500px']
                            }
                        });
                    },
                    shopCar: function (e) {
                        self._openTplFull({
                            tag: 'shopCar_order',
                            tagData: e,
                            index: 2,
                            openOpts: {
                                title: '我的购物车',
                                maxmin: true
                                // area: ['900px', '500px']
                            }
                        });
                    },
                    addCar: function (index) {
                        //购物车
                        fly(index);
                    },
                    search: function () {
                        this.hidden = !this.hidden;
                    },
                    reset: function () {
                        let that = this;
                        // that.formInline.GWAK001 = '';
                        // that.formInline.GWAKL003 = '';
                        that.formInline.GWDC001 = '';
                        //that.formInline.GWAK061 = '';
                        that.formInline.GWAM004 = '';
                        that.formInline.GWAM004_NAME = '';
                        that.formInline.GWAM007 = '';
                        that.formInline.GWAM007_NAME = '';
                        that.formInline.GWAML004 = '';
                        that.formInline.GWAM001 = '';
                    },
                    onSubmit: function () {
                        let that = this;
                        app.ajax({
                            xdata: {
                                GWDB012: that.formInline.GWAK001,
                                GWDC001: that.formInline.GWDC001,
                                GWAK061: that.formInline.GWAK061,
                                GWAM004: that.formInline.GWAM004,
                                GWAM007: that.formInline.GWAM007,
                                GWAML004: that.formInline.GWAML004,
                                GWAM001: that.formInline.GWAM001,
                                GWAK010: that.formInline.GWAK010
                            },
                            xurl: "admin/getGwamList",
                            cb: function (result) {
                                if (result.ok) {
                                    that.shopping = result.data.data;
                                    that.total4 = result.data.data.length;
                                    that.handleCurrentChange(1);
                                }
                            }
                        });
                    },
                    openKh: function (obj) {
                        let that = this;
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwak001',
                                    columnList: [{label: '客户编号', prop: 'GWAK001', width: '150'},
                                        {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                        {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                    ],
                                },
                                cb: function (data) {
                                    that.formInline.GWAK001 = data.GWAK001;
                                    that.formInline.GWAKL003 = data.GWAKL003;
                                    that.formInline.GWAK061 = data.GWAK061;
                                    that.formInline.GWAK010 = data.GWAK010;
                                }
                            },
                            index: 3,
                            openOpts: {
                                title: '选择客户',
                                area: ['600px', '470px']
                            }
                        });
                    },
                    openGwdc001: function (obj) {
                        let that = this;
                        self._openTplFull({
                            tag: 'common_open_gwdc001',
                            tagData: {
                                selectType: "singleTable",
                                GWCS010: that.formInline.GWAK001,//经销商
                                GWCSDOCDATE: new Date().Format('yyyy-MM-dd'),//单据日期
                                GWCS117: '',//订单类型
                                cb: function (data) {
                                    that.formInline.GWDC001 = data.GWDC001;
                                }
                            },
                            index: 4,
                            openOpts: {
                                title: '选择活动',
                                area: ['600px', '430px']
                            }
                        });
                    },
                    openGwam004: function (obj) {
                        let that = this;
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwao001',
                                    columnList: [{label: '系列编号', prop: 'GWAO001', width: '150'},
                                        {label: '说明', prop: 'GWAOL003', width: '150'},
                                        {label: '助记码', prop: 'GWAOL004', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    that.formInline.GWAM004 = data.GWAO001;
                                    that.formInline.GWAM004_NAME = data.GWAO001 + '(' + data.GWAOL003 + ')';
                                }
                            },
                            index: 3,
                            openOpts: {
                                title: '选择产品系列',
                                area: ['600px', '470px']
                            }
                        });
                    },
                    openGwam007: function (obj) {
                        let that = this;
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwah001',
                                    columnList: [{label: '分类编号', prop: 'GWAH001', width: '150'},
                                        {label: '说明', prop: 'GWAHL003', width: '150'},
                                        {label: '助记码', prop: 'GWAHL004', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    that.formInline.GWAM007 = data.GWAH001;
                                    that.formInline.GWAM007_NAME = data.GWAH001 + '(' + data.GWAHL003 + ')';
                                }
                            },
                            index: 3,
                            openOpts: {
                                title: '选择产品分类',
                                area: ['600px', '470px']
                            }
                        });
                    },
                    loadMsgList4() {
                        let that = this;
                        //list分页请求
                        app.ajax({
                            xurl: "admin/getUser",
                            cb: function (result) {
                                if (result.ok) {
                                    that.formInline.GWAK001 = result.data.data.GWAK001;
                                    that.formInline.GWAKL003 = result.data.data.GWAKL003;
                                    that.formInline.GWAK061 = result.data.data.GWAK061;
                                    app.ajax({
                                        xdata: {
                                            GWDB012: that.formInline.GWAK001,
                                            GWAK010: that.formInline.GWAK010
                                        },
                                        xurl: "admin/getGwamList",
                                        cb: function (result) {
                                            if (result.ok) {
                                                that.shopping = result.data.data;
                                                that.total4 = result.data.data.length;
                                                that.handleCurrentChange(1);
                                            }
                                        }
                                    });
                                }
                            }
                        });
                    },
                    loadMsgList3() {
                        let that = this;
                        //list分页请求
                        app.ajax({
                            xdata: {
                                GWDH002: '3'
                            },
                            xurl: "admin/getNotices",
                            cb: function (result) {
                                if (result.ok) {
                                    that.dList = result.data.data;
                                    that.nextPage3();
                                }
                            }
                        });
                    },
                    loadMsgList2() {
                        let that = this;
                        //list分页请求
                        app.ajax({
                            xdata: {
                                GWDH002: '2'
                            },
                            xurl: "admin/getNotices",
                            cb: function (result) {
                                if (result.ok) {
                                    that.cList = result.data.data;
                                    that.nextPage2();
                                }
                            }
                        });
                    },

                    loadMsgList() {
                        let that = this;
                        //list分页请求
                        app.ajax({
                            xdata: {
                                GWDH002: '1'
                            },
                            xurl: "admin/getNotices",
                            cb: function (result) {
                                if (result.ok) {
                                    that.lists = result.data.data;
                                    that.nextPage();
                                }
                            }
                        });
                    },
                    prevPage: function () {
                        if (this.lists.length > 0) {
                            if (this.currentPage > 1) {
                                this.currentPage--;
                            } else {
                                app.showWarning('没有上一页了');
                                return;
                            }
                            var start = (this.currentPage - 1) * this.pageSize;
                            var end = start + this.pageSize;
                            if (end > this.lists.length) {
                                end = this.lists.length;
                            }
                            this.pageData.splice(0, this.pageSize);
                            for (var i = start; i < end; i++) {
                                this.pageData.push(this.lists[i]);
                            }
                        }
                    },
                    nextPage: function () {
                        if (this.lists.length > 0) {
                            if (this.currentPage * this.pageSize < this.lists.length) {
                                this.currentPage++;
                            } else {
                                app.showWarning('没有下一页了');
                                return;
                            }
                            var start = (this.currentPage - 1) * this.pageSize;
                            var end = start + this.pageSize;
                            if (end > this.lists.length) {
                                end = this.lists.length;
                            }
                            this.pageData.splice(0, this.pageSize);
                            for (var i = start; i < end; i++) {
                                this.pageData.push(this.lists[i]);
                            }
                        }
                    },
                    prevPage2: function () {
                        if (this.cList.length > 0) {
                            if (this.currentPage2 > 1) {
                                this.currentPage2--;
                            } else {
                                app.showWarning('没有上一页了');
                                return;
                            }
                            var start = (this.currentPage2 - 1) * this.pageSize;
                            var end = start + this.pageSize;
                            if (end > this.cList.length) {
                                end = this.cList.length;
                            }
                            this.pageData2.splice(0, this.pageSize);
                            for (var i = start; i < end; i++) {
                                this.pageData2.push(this.cList[i]);
                            }
                        }
                    },
                    nextPage2: function () {
                        if (this.cList.length > 0) {
                            if (this.currentPage2 * this.pageSize < this.cList.length) {
                                this.currentPage2++;
                            } else {
                                app.showWarning('没有下一页了');
                                return;
                            }
                            var start = (this.currentPage2 - 1) * this.pageSize;
                            var end = start + this.pageSize;
                            if (end > this.cList.length) {
                                end = this.cList.length;
                            }
                            this.pageData2.splice(0, this.pageSize);
                            for (var i = start; i < end; i++) {
                                this.pageData2.push(this.cList[i]);
                            }
                        }
                    },
                    prevPage3: function () {
                        if (this.dList.length > 0) {
                            if (this.currentPage3 > 1) {
                                this.currentPage3--;
                            } else {
                                app.showWarning('没有上一页了');
                                return;
                            }
                            var start = (this.currentPage3 - 1) * this.pageSize;
                            var end = start + this.pageSize;
                            if (end > this.dList.length) {
                                end = this.dList.length;
                            }
                            this.pageData3.splice(0, this.pageSize);
                            for (var i = start; i < end; i++) {
                                this.pageData3.push(this.dList[i]);
                            }
                        }
                    },
                    nextPage3: function () {
                        if (this.dList.length > 0) {
                            if (this.currentPage3 * this.pageSize < this.dList.length) {
                                this.currentPage3++;
                            } else {
                                app.showWarning('没有下一页了');
                                return;
                            }
                            var start = (this.currentPage3 - 1) * this.pageSize;
                            var end = start + this.pageSize;
                            if (end > this.dList.length) {
                                end = this.dList.length;
                            }
                            this.pageData3.splice(0, this.pageSize);
                            for (var i = start; i < end; i++) {
                                this.pageData3.push(this.dList[i]);
                            }
                        }
                    },
                    prevPage4: function () {
                        if (this.shopping.length > 0) {
                            if (this.currentPage4 > 1) {
                                this.currentPage4--;
                            } else {
                                app.showWarning('没有上一页了');
                                return;
                            }
                            var start = (this.currentPage4 - 1) * this.pageSize4;
                            var end = start + this.pageSize4;
                            if (end > this.shopping.length) {
                                end = this.shopping.length;
                            }
                            this.pageData4.splice(0, this.pageSize4);
                            for (var i = start; i < end; i++) {
                                this.pageData4.push(this.shopping[i]);
                            }
                        }
                    },
                    nextPage4: function () {
                        if (this.shopping.length > 0) {
                            if (this.currentPage4 * this.pageSize4 < this.shopping.length) {
                                this.currentPage4++;
                            } else {
                                app.showWarning('没有下一页了');
                                return;
                            }
                            var start = (this.currentPage4 - 1) * this.pageSize4;
                            var end = start + this.pageSize4;
                            if (end > this.shopping.length) {
                                end = this.shopping.length;
                            }
                            this.pageData4.splice(0, this.pageSize4);
                            for (var i = start; i < end; i++) {
                                this.pageData4.push(this.shopping[i]);
                            }
                        }
                    },
                    open: function (item) {
                        self._openTplFull({
                            tag: 'notice_detail',
                            tagData: item.GWDHDOCNO,
                            index: 0,
                            openOpts: {
                                title: '公告资料详情',
                                maxmin: true
                                // area: ['700px', '500px']
                            }
                        });
                    },
                    handleCurrentChange: function (val) {
                        var start = (val - 1) * this.pageSize4;
                        var end = start + this.pageSize4;
                        if (end > this.shopping.length) {
                            end = this.shopping.length;
                        }
                        this.pageData4.splice(0, this.pageSize4);
                        /*for (var i = start; i < end; i++) {
                         this.pageData4.push(this.shopping[i]);
                         }*/
                        for (var i = start; i < end; i++) {
                            var url = null;
                            if (this.shopping[i].IMGURL) {
                                url = '/wissun-smfile/' + this.shopping[i].IMGURL.split(',')[0];
                            }
                            this.shopping[i].url = url;
                            // this.shopping[i].IMGURL = '/wissun-smfile/' + this.shopping[i].IMGURL.split(',')[0];
                            this.pageData4.push(this.shopping[i]);
                        }
                    }
                },
                mounted: function () {
                    this.loadMsgList();
                    this.loadMsgList2();
                    this.loadMsgList3();
                    this.loadMsgList4();
                },
                updated: function () {
                    if (this.activeName2 == 'first') {
                        $('#rightBtn2').css('display', 'none');
                        $('#rightBtn').css('display', 'block');
                        let nextbtn = $('#rightBtn');
                        let headLayout = $('.el-tabs__nav-scroll');
                        nextbtn.prependTo(headLayout);
                    } else if (this.activeName2 == 'second') {
                        $('#rightBtn').css('display', 'none');
                        $('#rightBtn2').css('display', 'block');
                        let nextbtn = $('#rightBtn2');
                        let headLayout = $('.el-tabs__nav-scroll');
                        nextbtn.prependTo(headLayout);
                    }
                }
            });
            self.initData(1);


        },
        _lazy: function () {
            this._super();

        }
    });
    //添加购物车
    function fly(index) {
        var eleBtn = index.currentTarget;
        var eleCart = document.querySelector('#shopCart');
        var eleParent = eleBtn.offsetParent.parentElement;

        var isRunning = false;
        // 现在按钮距离购物车的距离
        var boundBtn = eleBtn.getBoundingClientRect();
        var boundCart = eleCart.getBoundingClientRect();
        var eleFlyItem = eleParent.querySelector('.crm_fly_item');
        var eleFlyImg = eleFlyItem.querySelector('img');
        //购物车图形出现
        eleFlyItem.style.display = 'block';

        // 中心点的水平垂直距离
        var offsetX = boundCart.left + boundCart.width / 2 - (boundBtn.left + boundBtn.width / 2) - 70;
        var offsetY = boundCart.top + boundCart.height / 2 - (boundBtn.top + boundBtn.height / 2);
        // 页面滚动尺寸
        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop || 0;
        var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft || 0;


        if (isRunning == false) {
            // 初始定位
            eleFlyItem.style.left = (boundBtn.left + scrollLeft + this.clientWidth / 2) - 140 + 'px';
            eleFlyItem.style.top = (boundBtn.top + scrollTop + this.clientHeight / 2) - 140 + 'px';
            // 开始动画
            eleFlyItem.style.transform = 'translateX(' + offsetX + 'px)';
            eleFlyImg.style.transform = 'translateY(' + offsetY + 'px)';

            // 动画标志量
            isRunning = true;
            setTimeout(function () {
                eleFlyItem.style.display = '';
                eleFlyItem.style.transform = 'translateX(0)';
                eleFlyImg.style.transform = 'translateY(0)';
                isRunning = false;
                eleCart.querySelector('span').innerHTML = eleCart.querySelector('span').innerHTML * 1 + 1;
            }, 490);
        }


    }

    return new Admin();

});
